package com.kunbo.cn.app.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import com.kunbo.cn.app.dao.CarDao;
import com.kunbo.cn.app.entity.Car;
import com.kunbo.cn.app.service.CarService;

@Service
public class CarImpl implements CarService{
	
	@Autowired
	private CarDao dao;
	@Autowired
	private JdbcTemplate jdbc;
	

	@Override
	public Boolean saveCar(String number, String RFID_number, String license_plate_number, String vehicle_type,
			String handling_type, String maximum_payload, String tare, String headweight, String total_traction_weight,
			String road_transpor_certificate,String unit,String uuid,String create_user_id) {
		// TODO Auto-generated method stu
		Car car = new Car();
		car.setNumber(number);
		car.setRFID_number(RFID_number);
		car.setLicense_plate_number(license_plate_number);
		car.setVehicle_type(vehicle_type);
		car.setHandling_type(handling_type);
		car.setMaximum_payload(maximum_payload);
		car.setTare(tare);
		car.setHeadweight(headweight);
		car.setTotal_traction_weight(total_traction_weight);
		car.setRoad_transpor_certificate(road_transpor_certificate);
		car.setSubordinate_transport_units(unit);
		car.setUuid(uuid);
		car.setCreate_user_id(create_user_id);
		car.setCar_state("空闲");
		car = dao.save(car);
		if(car != null)
			return true;
		else
			return false;
	}

	@Override
	public Car selById(Integer id) {
		// TODO Auto-generated method stub
		return dao.findOne(id);
	}

	@Override
	public Boolean updateCar(Integer id, String number, String RFID_number, String license_plate_number,
			String vehicle_type, String handling_type, String maximum_payload, String tare, String headweight,
			String total_traction_weight, String road_transpor_certificate,Integer unit) {
		// TODO Auto-generated method stub
		Car car = dao.findOne(id);
		car.setNumber(number);
		car.setRFID_number(RFID_number);
		car.setLicense_plate_number(license_plate_number);
		car.setVehicle_type(vehicle_type);
		car.setHandling_type(handling_type);
		car.setMaximum_payload(maximum_payload);
		car.setTare(tare);
		car.setHeadweight(headweight);
		car.setTotal_traction_weight(total_traction_weight);
		car.setRoad_transpor_certificate(road_transpor_certificate);
		car.setSubordinate_transport_units(unit+"");
		car = dao.save(car);
		if(car != null)
			return true;
		else
			return false;
	}

	@Override
	public Boolean delCar(Integer id) {
		// TODO Auto-generated method stub
		dao.delete(id);
		Car car = dao.findOne(id);
		if(car == null)
			return true;
		else
			return false;
	}

	@Override
	public List<Map<String, Object>> selCar(String carNumber,Integer page,Integer pageSize,Integer wayBillUnit) {
		// TODO Auto-generated method stub
		
		String sql = "select * from vehicle_management where 1 = 1 ";
		if(carNumber != null && !carNumber.equals("")) {
			sql += " and license_plate_number like '%" + carNumber + "%'";
		}
		sql += " and subordinate_transport_units = " + wayBillUnit;
		sql += " order by id desc limit " + (page*pageSize) + "," + pageSize;
		List<Map<String,Object>> list = jdbc.queryForList(sql);
//		for (Map<String, Object> map : list) {
//			int vehicle_type = Integer.parseInt(map.get("vehicle_type").toString());
//			sql = "select text from select_car_type where id = " + vehicle_type;
//			List<Map<String,Object>> lists = jdbc.queryForList(sql);
//			map.put("vehicle_type", lists.get(0).get("text").toString());
//			int handling_type = Integer.parseInt(map.get("handling_type").toString());
//			sql = "select text from select_zhuangxieleixing where id = " + handling_type;
//			List<Map<String,Object>> listes = jdbc.queryForList(sql);
//			map.put("handling_type", listes.get(0).get("text").toString());
//			sql = "select name_of_transport_unit from transport_unit_management where id = " + Integer.parseInt(map.get("subordinate_transport_units").toString());
//			map.put("subordinate_transport_units", jdbc.queryForList(sql).get(0).get("name_of_transport_unit").toString());
//		}
		
		return list;
	}

	@Override
	public List<Map<String, Object>> selCarNumber(String carNumber,Integer wayBillUnit) {
		// TODO Auto-generated method stub
		String sql = "select * from vehicle_management where license_plate _number = '" + carNumber+ "' and  subordinate_transport_units = " +wayBillUnit + " order by id desc";
		return jdbc.queryForList(sql);
	}

	@Override
	public String selByCarId(Integer id) {
		// TODO Auto-generated method stub
		String sql = "select  maximum_payload as text from vehicle_management where id = " + id;
		List<Map<String, Object>> list = jdbc.queryForList(sql);
		return list.get(0).get("text").toString();
	}

}
 